<?php

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Mod_Location extends CI_Model
{ 
  public function getLocation($start, $num)
  {
    $this->db->select('*')
        ->from(table('location'))
        ->order_by(field('updatedAt'), 'DESC');

    $this->db->limit($num, $start);

    return $this->db->get();
  }
  
  public function getLocationNum()
  {
    $objDb = $this->getAllLocation();
    return $objDb->num_rows();
  }
  
  public function getAllLocation() {
    $this->db->select('*')
        ->from(table('location'))
        ->order_by(field('updatedAt'), 'DESC');

    return $this->db->get();
  }
  
  public function getLocationById($id)
  {
    $this->db->select('*')
        ->from(table('location'))
        ->order_by(field('updatedAt'), 'DESC')
        ->where(field('id'), $id);

    $result = $this->db->get();

    return $result->row_array();
  }
  
  public function insert()
  {
    $data = array(
      field('name')       => $this->input->post('name'),
      field('description')=> $this->input->post('description')
    );
    
    $this->db->insert(table('location'), $data); 
    return $this->db->affected_rows();
  }
  
  public function update($id)
  {
    $data = array(
      field('name')       => $this->input->post('name'),
      field('description')=> $this->input->post('description')
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
    
    $this->db->where(field('id'),$id);
    $this->db->update(table('location'), $data);

    return $this->db->affected_rows();
  }
  
  public function delete($id)
  {
    $this->db->where(field('id'), $id);
    $this->db->delete(table('location'));

    return $this->db->affected_rows();
  }
}